package com.example.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends Activity {

    private static final String SSO_RESOURCE_URL =
            "https://sso.douyin.com/?service=https://www.douyin.com/home?scene=douyin_mobile";

    private WebView mWebView;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.setWebViewClient(new WriteHandlingWebViewClient(mWebView));
        // 注入Native对象到webview window中
        mWebView.addJavascriptInterface(this, "LinkNative");
        // debug
        WebView.setWebContentsDebuggingEnabled(true);
        // file:// 跨域问题
        mWebView.getSettings().setAllowFileAccessFromFileURLs(true);

        // 是否可以缩放，默认true
        webSettings.setSupportZoom(true);
        // 是否显示缩放按钮，默认false
        webSettings.setBuiltInZoomControls(true);
        // 设置此属性，可任意比例缩放。大视图模式
        webSettings.setUseWideViewPort(true);
        // 和setUseWideViewPort(true)一起解决网页自适应问题
        webSettings.setLoadWithOverviewMode(true);
        // 是否使用缓存
        webSettings.setAppCacheEnabled(true);
        // 开启本地DOM存储
        webSettings.setDomStorageEnabled(true);
        // 加载图片
        webSettings.setLoadsImagesAutomatically(true);
        // 播放音频，多媒体需要用户手动？设置为false为可自动播放
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        // 允许访问文件
        webSettings.setAllowFileAccess(true);

        // REMOTE RESOURCE
        mWebView.loadUrl(SSO_RESOURCE_URL);
    }

    @JavascriptInterface
    public void jsonrpc(String param) {
        Log.i("[INFO]jsonrpc:", param.toString());
        nativeCallJs(param);
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public void nativeCallJs(String parameters) {
        String jsStr = "javascript:linkNativeCallBack(" + parameters + ")";
        Log.i("[INFO]nativeCallJs:", jsStr.toString());
        // js
        mWebView.post(new Runnable() {
            @Override
            public void run() {
                mWebView.evaluateJavascript(jsStr, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        // 此处为 js 返回的结果
                        Log.i("[INFO]onReceiveValue:", value.toString());
                    }
                });
            }
        });
    }
}
